CLASS lhc_ZI_INVOICE_HEAD DEFINITION INHERITING FROM cl_abap_behavior_handler.
  PRIVATE SECTION.

    METHODS get_instance_authorizations FOR INSTANCE AUTHORIZATION
      IMPORTING keys REQUEST requested_authorizations FOR zi_invoice_head RESULT result.

    METHODS read FOR READ
      IMPORTING keys FOR READ zi_invoice_head RESULT result.

    METHODS lock FOR LOCK
      IMPORTING keys FOR LOCK zi_invoice_head.
    METHODS adjust_post FOR MODIFY
      IMPORTING keys FOR ACTION zi_invoice_head~adjust_post RESULT result.

ENDCLASS.

CLASS lhc_ZI_INVOICE_HEAD IMPLEMENTATION.

  METHOD get_instance_authorizations.
  ENDMETHOD.

  METHOD read.
  ENDMETHOD.

  METHOD lock.
  ENDMETHOD.

  METHOD adjust_post.

    DATA: lt_je_deep    TYPE TABLE FOR ACTION IMPORT i_journalentrytp~post,
          lv_cid        TYPE abp_behv_cid,
          lv_index      TYPE i,
          lv_account_sr TYPE hkont.
    DATA:lt_header_data TYPE TABLE FOR CREATE zi_generaljournalentry,
         lt_item_data   TYPE TABLE FOR CREATE zi_generaljournalentry\_Item,
         ls_item_data   LIKE LINE OF lt_item_data.

    DATA(lv_BillingDocument) = keys[ 1 ]-BillingDocument.

    SELECT
     *
     FROM zi_invoice_head
     WITH PRIVILEGED ACCESS
     WHERE BillingDocument = @lv_BillingDocument
     INTO TABLE @DATA(lt_invoice_head).
    READ TABLE lt_invoice_head INTO DATA(ls_invoice_head) INDEX 1.
    SELECT
      *
      FROM ZI_INVOICE_item
      WITH PRIVILEGED ACCESS
      WHERE BillingDocument = @lv_BillingDocument
      INTO TABLE @DATA(lt_INVOICE_item).

    SELECT
      AccountingDocument,
      companycode,
      FiscalYear,
      glaccount,
      amountincompanycodecurrency,
      referencedocument
      FROM  I_glaccountlineitem
      WITH PRIVILEGED ACCESS
      WHERE referencedocument = @lv_BillingDocument
      AND SourceLedger = '0L'
      AND AccountingDocumentType = 'RV'
      AND glaccount BETWEEN '6001010110' AND '6001011000'
      INTO TABLE @DATA(lt_glaccountlineitem).

    TRY.
        lv_cid = to_upper( cl_uuid_factory=>create_system_uuid( )->create_uuid_x16( ) ).
      CATCH cx_uuid_error.
        ASSERT 1 = 0.
    ENDTRY.

    APPEND INITIAL LINE TO lt_je_deep ASSIGNING FIELD-SYMBOL(<je_deep>).
    <je_deep>-%cid = lv_cid.
    <je_deep>-%param = VALUE #( companycode = ls_invoice_head-SalesOrganization
                                documentreferenceid = 'BKPFF'
                                createdbyuser = sy-uname
                                businesstransactiontype = 'RFBU'
                                accountingdocumenttype = 'RV'
                                documentdate = sy-datlo
                                postingdate = sy-datlo
                                accountingdocumentheadertext = ls_invoice_head-BillingDocument
    ).
    lv_index = 0.
    LOOP AT lt_INVOICE_item INTO DATA(ls_item).
      lv_index = lv_index + 1.
      READ TABLE lt_glaccountlineitem INTO DATA(ls_glaccountlineitem) WITH KEY referencedocument = ls_item-BillingDocument.
      IF sy-subrc = 0.
        CASE ls_glaccountlineitem-glaccount.
          WHEN '6001010110'.
            lv_account_sr = '6001010100'.
          WHEN '6001010210'.
            lv_account_sr = '6001010200'.
          WHEN '6001010310'.
            lv_account_sr = '6001010300'.
          WHEN '6001010410'.
            lv_account_sr = '6001010400'.
          WHEN '6001010510'.
            lv_account_sr = '6001010500'.
        ENDCASE.
*        APPEND VALUE #(  glaccountlineitem = lv_index
*                         glaccount = ls_glaccountlineitem-glaccount
*                         _currencyamount = VALUE #( ( currencyrole = '00' journalentryitemamount = ls_glaccountlineitem-amountincompanycodecurrency currency = 'CNY' ) )
*                         )
*                       TO <je_deep>-%param-_glitems.
        APPEND VALUE #( %cid = |Item{ lv_index }|
                        uuid =  lv_cid
                        buzei = lv_index
                        hkont = ls_glaccountlineitem-glaccount
                        wrbtr = ls_glaccountlineitem-amountincompanycodecurrency
                        Waers = 'CNY' ) TO ls_item_data-%target.
        lv_index = lv_index + 1.
        APPEND VALUE #( %cid = |Item{ lv_index }|

                        uuid =  lv_cid
                        buzei = lv_index
                        hkont = lv_account_sr
                        wrbtr = - ls_glaccountlineitem-amountincompanycodecurrency
                        Waers = 'CNY' ) TO ls_item_data-%target.
        ls_item_data-%cid_ref = 'H001'.

ENDIF.
      ENDLOOP.



      APPEND INITIAL LINE TO lt_header_data ASSIGNING FIELD-SYMBOL(<fs_header>).
      <fs_header> = VALUE #( %key-Uuid = lv_cid
                              Bukrs = ls_invoice_head-SalesOrganization
                              Gjahr = sy-datum+0(4)
                              waers = 'CNY'
                              budat = sy-datlo
                              bldat = sy-datlo
                              Uuid = lv_cid
                              %cid = 'H001'
                              ) .

      ls_item_data-Uuid =  lv_cid.
*    ls_item_data-%cid_ref = lv_cid.
      ls_item_data-%key-Uuid = lv_cid.
      APPEND ls_item_data TO lt_item_data.


      MODIFY ENTITIES OF zi_generaljournalentry
            ENTITY ZI_GeneralJournalEntry
             CREATE SET FIELDS WITH CORRESPONDING #( lt_header_data )
             CREATE BY \_Item SET FIELDS WITH CORRESPONDING #( lt_item_data )
            FAILED DATA(ls_failed_deep)
            REPORTED DATA(ls_reported_deep)
            MAPPED DATA(ls_mapped_deep).
      IF sy-subrc = 0.
      ENDIF.

    ENDMETHOD.

ENDCLASS.

CLASS lsc_ZI_INVOICE_HEAD DEFINITION INHERITING FROM cl_abap_behavior_saver.
PROTECTED SECTION.

  METHODS finalize REDEFINITION.

  METHODS check_before_save REDEFINITION.

  METHODS save REDEFINITION.

  METHODS cleanup REDEFINITION.

  METHODS cleanup_finalize REDEFINITION.

ENDCLASS.

CLASS lsc_ZI_INVOICE_HEAD IMPLEMENTATION.

METHOD finalize.
ENDMETHOD.

METHOD check_before_save.
ENDMETHOD.

METHOD save.
ENDMETHOD.

METHOD cleanup.
ENDMETHOD.

METHOD cleanup_finalize.
ENDMETHOD.

ENDCLASS.
